<?php 
class ControllerCheckoutGlobal extends Controller {
	public function index() {
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/global.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/deploy/global.tpl';
		} else {
			$this->template = 'default/template/deploy/basic.tpl';
		}


        $this->data['regions']=array(
            array(
                "value"=>"qingcloud_pek2",
                'name'  =>"qingcloud_pek2"
            ),

            array(
                "value"=>"qingcloud_gd1",
                'name'  =>"qingcloud_gd1"
            ),
        );

		$this->response->setOutput($this->render());			
	}

	
	public function validate() {
		$this->language->load('checkout/checkout');

		$json = array();

		// Validate if customer is logged in.
		if (!$this->customer->isLogged()) {
			$json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
		}

		// Validate cart has products and has stock.
		if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
			$json['redirect'] = $this->url->link('checkout/cart');
		}	

		// Validate minimum quantity requirments.			
		$products = $this->cart->getProducts();

		foreach ($products as $product) {
			$product_total = 0;

			foreach ($products as $product_2) {
				if ($product_2['product_id'] == $product['product_id']) {
					$product_total += $product_2['quantity'];
				}
			}		

			if ($product['minimum'] > $product_total) {
				$json['redirect'] = $this->url->link('checkout/cart');

				break;
			}
		}

		if (!$json) {
            if ($this->check->check_str_len($this->request->post['default_admin'], 8, 32) || (!$this->check->check_up_low_number_exist($this->request->post['default_admin']))) {
                $json['error']['default_admin'] = "���������������������������������������8-32������,���������������������������������������������������";
            }
            
            if ($this->check->check_cloud_region($this->request->post['region'])){
            	$json['error']['region'] = "the input region is not valid";
            }
        }

        if (!$json) {
            $this->session->data['app']['global']['default_admin'] = trim($this->request->post['default_admin']);
            $this->session->data['app']['global']['region'] = $this->request->post['region'];
        }

		$this->response->setOutput(json_encode($json));
	}

}
?>